//#define __DEBUG__
// by Xeno
#define THIS_FILE "x_revive\xr_f\fn_dragprone.sqf"
#include "xr_macros.sqf"

private ["_dragee", "_unit", "_pullanims"];
_dragee = __pGetVar(GVARXR(cursorTarget));

if (isNull _dragee || {!alive player}) exitWith {};

__pSetVar [QGVARXR(pisinaction), true];
__pSetVar [QGVARXR(is_dragging), true];

_unit = player;
private "_name_dragee";
_name_dragee = (localize "STR_DOM_MISSIONSTRING_906");
if (alive _dragee && {name _dragee != (localize "STR_DOM_MISSIONSTRING_906")}) then {_name_dragee = name _dragee};

_dragee setVariable [QGVARXR(dragged), true, true];
 
sleep 2;

[QGVARXR(a4), _dragee] call d_fnc_NetCallEvent;

_dragee attachto [_unit, [0, 2, 0]];
sleep 0.02;
[QGVARXR(sd), _dragee] call d_fnc_NetCallEvent;

GVARXR(drag) = true;

if (GVARXR(dropAction) != -3333) then {__PRA(GVARXR(dropAction));GVARXR(dropAction) = -3333};
GVARXR(dropAction) = player addAction [format["<t color='#FF0000'>Drop %1</t>",_name_dragee], {_this call FUNCXR(drop_body)}, [_dragee, 1], 0, false, true];
GVARXR(dragprone_keyDownEHId) = (findDisplay 46) displayAddEventHandler ["KeyDown", {_this call FUNCXR(dragkeydownprone)}];
sleep 0.5;

_found_anim = false;

_pullanims = ["amovppnemstpsraswrfldnon","amovppnemrunslowwrfldf","amovppnemsprslowwrfldfl","amovppnemsprslowwrfldfr","amovppnemrunslowwrfldb","amovppnemsprslowwrfldbl","amovppnemsprslowwrfldr","amovppnemstpsraswrfldnon_turnl","amovppnemstpsraswrfldnon_turnr","amovppnemrunslowwrfldl","amovppnemrunslowwrfldr","amovppnemsprslowwrfldb","amovppnemrunslowwrfldbl","amovppnemsprslowwrfldl","amovppnemsprslowwrfldbr"];

while {GVARXR(drag)} do {
	if (!_found_anim && {animationState player in _pullanims}) then {
		_found_anim = true;
	};
	if (!alive _dragee || {!__GV(_dragee,GVARXR(pluncon))}) exitWith {
		__PRA(GVARXR(dropAction));
		GVARXR(dropAction) = -3333;
		detach _dragee;
		sleep 0.5;
		if ((alive _dragee && {__GV(_dragee,GVARXR(pluncon))}) || {!alive _dragee}) then {
			[QGVARXR(wn), [_dragee,101]] call d_fnc_NetCallEvent;
		} else {
			[QGVARXR(wn), [_dragee,102]] call d_fnc_NetCallEvent;
		};
		sleep 1;
		GVARXR(drag) = false;
	};
	
	if (_found_anim && {!((animationState _unit) in _pullanims)}) exitWith {
		__PRA(GVARXR(dropAction));
		GVARXR(dropAction) = -3333;
		detach _dragee;
		sleep 0.5;
		if ((alive _dragee && {__GV(_dragee,GVARXR(pluncon))}) || {!alive _dragee}) then {
			[QGVARXR(wn), [_dragee,101]] call d_fnc_NetCallEvent;
		} else {
			[QGVARXR(wn), [_dragee,102]] call d_fnc_NetCallEvent;
		};
		sleep 1;
		GVARXR(drag) = false;
	};

	if (!alive _unit) exitWith {
		__PRA(GVARXR(dropAction));
		GVARXR(dropAction) = -3333;
		detach _unit;
		if ((alive _dragee && {__GV(_dragee, GVARXR(pluncon))}) || {!alive _dragee}) then {
			[QGVARXR(wn), [_dragee,101]] call d_fnc_NetCallEvent;
		} else {
			[QGVARXR(wn), [_dragee,102]] call d_fnc_NetCallEvent;
		};
		sleep 1;
		GVARXR(drag) = false;
	};
	
	if (isNull _dragee) then {
		__PRA(GVARXR(dropAction));
		GVARXR(dropAction) = -3333;
		detach _unit;
		sleep 1;
		GVARXR(drag) = false;
	};
	
	sleep 0.1;
};

__pSetVar [QGVARXR(pisinaction), false];
__pSetVar [QGVARXR(is_dragging), false];
_dragee setVariable [QGVARXR(dragged), false, true];

(findDisplay 46) displayRemoveEventHandler ["KeyDown", GVARXR(dragprone_keyDownEHId)];

if (GVARXR(dropAction) != -3333) then {
	__PRA(GVARXR(dropAction));
	GVARXR(dropAction) = -3333;
};